package cn.sxgan.random;


import cn.sxgan.FilePathConst;
import cn.sxgan.utils.CommonUtils;
import cn.sxgan.utils.FileUtils;
import cn.sxgan.utils.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @Description: 随机工具类
 * @Author: sxgan
 * @Date: 2024-08-30 14:00
 * @Version: 1.0
 **/

public class RandomUtils {
    private static final SecureRandom RANDOM = new SecureRandom();
    private static BulkNames bulkNames;
    private static final String[] EMAIL_SUFFIX = {"aliyun.com", "huawei.com", "foxmail.com", "qq.com", "163.com", "gmail.com", "hotmail.com", "outlook.com", "yahoo.com"};
    /**
     * 中国移动
     */
    private static final String[] CHINA_MOBILE = {
            "134", "135", "136", "137", "138", "139", "150", "151", "152", "157", "158", "159",
            "182", "183", "184", "187", "188", "178", "147", "172", "198"
    };
    /**
     * 中国联通
     */
    private static final String[] CHINA_UNICOM = {"130", "131", "132", "145", "155", "156", "166", "171", "175", "176", "185", "186", "166"};
    
    /**
     * 中国电信
     */
    private static final String[] CHINA_TELECOM = {"133", "149", "153", "173", "177", "180", "181", "189", "199"};
    
    static {
        String str = FileUtils.jsonFileToJsonStr(FilePathConst.USER_NAME_FILE_DIR);
        bulkNames = JsonUtils.parseObject(str, BulkNames.class);
    }
    
    /**
     * 随机获取一个中文名
     *
     * @return 中文名
     */
    public static String generateChineseName() {
        String[] surnames = bulkNames.getSurnames();
        String[] givenNames = bulkNames.getGivenNames();
        // 随机挑选一个姓氏
        String surname = surnames[RANDOM.nextInt(surnames.length)];
        // 随机挑选名称
        StringBuilder givenName = new StringBuilder(givenNames[RANDOM.nextInt(givenNames.length)]);
        for (int i = 0; i < RANDOM.nextInt(2); i++) {
            givenName.append(givenNames[RANDOM.nextInt(givenNames.length)]);
        }
        return surname + givenName;
    }
    
    /**
     * 随机生成指定个数的中文名，上限为1000
     */
    public static List<String> generateChineseName(int num) {
        List<String> names = new ArrayList<>();
        if (num <= 0) {
            return names;
        }
        if (num > 1000) {
            num = 1000;
        }
        for (int i = 0; i < num; i++) {
            names.add(generateChineseName());
        }
        return names;
    }
    
    
    /**
     * 随机获取手机号
     *
     * @param operator 运营商
     */
    public static String createPhoneNumber(OperatorEnum operator) {
        // 定义StringBuilder对象用于存储生成的手机号
        StringBuilder builder = new StringBuilder();
        // 手机号前三位
        String mobilePrefix = null;
        // 随机生成指定运营商中的手机前三位
        switch (operator) {
            // 中国移动
            case CHINA_MOBILE:
                mobilePrefix = CHINA_MOBILE[RANDOM.nextInt(CHINA_MOBILE.length)];
                break;
            // 中国联通
            case CHINA_UNICOM:
                mobilePrefix = CHINA_UNICOM[RANDOM.nextInt(CHINA_UNICOM.length)];
                break;
            // 中国电信
            case CHINA_TELECOM:
                mobilePrefix = CHINA_TELECOM[RANDOM.nextInt(CHINA_TELECOM.length)];
                break;
            default:
                mobilePrefix = "运营商错误";
                break;
        }
        ;
        // 拼接手机号前三位
        builder.append(mobilePrefix);
        // 定义辅助变量用于手机号后八位的生成
        int temp;
        // 生成手机号后8位
        for (int i = 0; i < 8; i++) {
            // 随机生成一个 [0, 9] 以内的整数
            temp = RANDOM.nextInt(10);
            // 拼接当前随机数
            builder.append(temp);
        }
        // 将生成的电话号码返回
        return builder.toString();
    }
    
    /**
     * 随机生成邮箱
     *
     * @return 邮箱
     */
    public static String createEmail() {
        StringBuilder sb = new StringBuilder();
        sb.append(CommonUtils.generateRandomCode(15).toLowerCase(Locale.ROOT));
        sb.append("@");
        sb.append(EMAIL_SUFFIX[RANDOM.nextInt(EMAIL_SUFFIX.length)]);
        return sb.toString();
    }
    
    @Data
    static class BulkNames {
        private String[] surnames;
        private String[] givenNames;
    }
    
    @Getter
    @AllArgsConstructor
    public enum OperatorEnum {
        
        /**
         * 中国移动
         */
        CHINA_MOBILE(0, "中国移动"),
        /**
         * 中国联通
         */
        CHINA_UNICOM(1, "中国联通"),
        /**
         * 中国电信
         */
        CHINA_TELECOM(2, "中国电信");
        
        /**
         * 运营商识别码
         */
        private final Integer code;
        
        /**
         * 运营商名称
         */
        private final String name;
    }
    
    // public static void main(String[] args) {
    //     // List<String> name = generateChineseName(1005);
    //     // name.forEach(System.out::println);
    //     for (int i = 0; i < 100; i++) {
    //         System.out.println(createEmail());
    //     }
    //
    // }
}
